/*
 * Hyuga: A Density-Grid Stream Clustering Platform.
 * Copyright (C) 2014 PUC-Rio/Laboratory for Advanced Collaboration
 *
 * Hyuga is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * Hyuga is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with Hyuga.  If not, see <http://www.gnu.org/licenses/>.
 */
package br.pucrio.inf.lac.konoha.hyuga.events.grid;

import java.util.Collection;
import java.util.HashSet;
import java.util.Set;

import br.pucrio.inf.lac.konoha.hyuga.common.Pair;
import br.pucrio.inf.lac.konoha.hyuga.events.CEPEvent;
import br.pucrio.inf.lac.konoha.hyuga.events.cell.CellClusterEvent;
import br.pucrio.inf.lac.konoha.hyuga.events.cell.CellContentEvent;

/**
 * Represent a grid cluster event. This event is formed by a set of context partition (cells) clusters that represent
 * the cluster, i.e., it is a cluster of contexts partition clusters. It contains the core partitions that form and
 * shapes the cluster. Note that this is not the final cluster, in addition to the core context partitions, the final
 * cluster contains the border partitions of these core partitions.
 * 
 * @author Marcos Roriz <mroriz@inf.puc-rio.br>
 */
public class GridLogicalClusterEvent extends CEPEvent {
    private Set<Pair<Integer, Integer>> coreCells;
    private long timestamp;

    // No arg constructor
    public GridLogicalClusterEvent() {
        this.coreCells = new HashSet<Pair<Integer,Integer>>();
    }
    
    /**
     * Grid (set of Cells/Partitions) that forms the cluster.
     * @param cells The core cells.
     */
    public GridLogicalClusterEvent(CellClusterEvent[] cells) {
        if (cells != null) {
            this.coreCells = new HashSet<Pair<Integer,Integer>>();
            
            for (CellClusterEvent cellCluster : cells) {
                // Adding Core Cells
                int x = cellCluster.getCore().getX();
                int y = cellCluster.getCore().getY();
                coreCells.add(new Pair<Integer, Integer>(x, y));
            }
        }
        
        this.type = TYPE.GRIDCLUSTER;
    }
    
    /**
     * Grid (set of Cells/Partitions) that forms the cluster.
     * @param cells The core cells.
     */
    public GridLogicalClusterEvent(Set<Pair<Integer, Integer>> coreCells) {
        this.coreCells = coreCells;
        this.type = TYPE.GRIDCLUSTER;
    }

    
    public boolean contains(int x, int y) {
        boolean contained = false;
        if (coreCells != null) {
            contained = coreCells.contains(new Pair<Integer, Integer>(x, y));
        }
        
        return contained;
    }
    
    public void removeCell(Pair<Integer, Integer> cell) {
        coreCells.remove(cell);
    }
    
    @Override
    public String toString() {
        String delim = "";
        String clusterCells = "";
        for (Pair<Integer, Integer> cell : coreCells) {
            clusterCells += delim + "(" + cell.getFirst() + "," + cell.getSecond() + ")";
            delim = ",";
        }
        return "CLUSTER @ " + clusterCells;
    }

    //////////////////////////////////////////////////////////////////////////
    // Gets and Sets
    //////////////////////////////////////////////////////////////////////////

    public Set<Pair<Integer, Integer>> getCoreCells() {
        return coreCells;
    }

    public long getTimestamp() {
        return timestamp;
    }

    public void setCoreCells(Set<Pair<Integer, Integer>> coreCells) {
        this.coreCells = coreCells;
    }

    public void setTimestamp(long timestamp) {
        this.timestamp = timestamp;
    }
}
